package bo;

import boerrors.AdresseMinimumMultiplicityException;
import boerrors.CodePostalMinimumMultiplicityException;
import java.rmi.RemoteException;
import java.util.Vector;
import run.PublisherImpl;

public class ListeClients extends PublisherImpl{
        private static ListeClients instance;    
        private Vector<ClientParticulier> _listeClientParticulier;
        private Vector<ClientSociete> _listeClientSociete;
        private Vector<Client> _listeClient;
    
       //*******************************************************************    
       //Constructeur du singleton    
       //******************************************************************* 

        public static ListeClients getInstance() throws RemoteException{
                if(instance == null) 
                        instance = new ListeClients();
                return instance;
        }    
    
	//*******************************************************************
	//Constructeurs
	//*******************************************************************
        
        private ListeClients() throws RemoteException{
                this._listeClientParticulier = new Vector<ClientParticulier>();
                this._listeClientSociete = new Vector<ClientSociete>();
                this._listeClient = new Vector<Client>();
        }    

        //*******************************************************************
	//Methodes
	//*******************************************************************        
        
        public synchronized ClientParticulier rechercherParticulierIdClient(int idClient) throws RemoteException {
                if (idClient > 0){
                        for(int i = 0 ; i < this._listeClientParticulier.size() ; i++){
                        ClientParticulier tampon = (ClientParticulier) this._listeClientParticulier.get(i);
                        if ((tampon.getIdClient()) == idClient)
                                return tampon;
                        }
                }
                return null;
        }    
    
        public synchronized Vector<ClientParticulier> rechercherParticulierNomPrenom(String nom, String prenom) throws RemoteException {
                Vector<ClientParticulier> _listeTampon = new Vector<ClientParticulier>();
                for(int i = 0; i < this._listeClientParticulier.size(); i++){
                    ClientParticulier tampon = (ClientParticulier) this._listeClientParticulier.get(i);
                    if ((tampon.getNom().contains(nom))&&(tampon.getPrenom().contains(prenom)))
                        _listeTampon.add(tampon);
                }
                return _listeTampon;
        }
        
        public synchronized Vector<ClientParticulier> rechercherParticulierAdresse( String rue, String numero, 
                                                                                    String codePostal, String commune) 
                                                                                    throws RemoteException, 
                                                                                    AdresseMinimumMultiplicityException, 
                                                                                    CodePostalMinimumMultiplicityException {
                Vector<ClientParticulier> _listeTampon = new Vector<ClientParticulier>();
                for(int i = 0; i < this._listeClientParticulier.size(); i++){
                    ClientParticulier tampon = (ClientParticulier) this._listeClientParticulier.get(i);
                    if ((tampon.getAdresse().getRue().contains(rue))&&(tampon.getAdresse().getNumero().contains(numero))&&
                        (tampon.getAdresse().getCodePostal().getCodePostal().contains(codePostal))&&
                        (tampon.getAdresse().getCodePostal().getVille().contains(commune)))
                        _listeTampon.add(tampon);
                }
                return _listeTampon;
        }

        public synchronized ClientSociete rechercherSocieteIdClient(int idClient) throws RemoteException {
                if (idClient > 0){
                        for(int i = 0 ; i < this._listeClientSociete.size() ; i++){
                        ClientSociete tampon = (ClientSociete) this._listeClientSociete.get(i);
                        if ((tampon.getIdClient()) == idClient)
                                return tampon;
                        }
                }
                return null;
        }    
     
    
        public synchronized ClientSociete rechercherSocieteNom(String nomSociete) throws RemoteException{
                if  (nomSociete != null){
                        for(int i = 0 ; i < this._listeClientSociete.size() ; i++){
                        ClientSociete tampon = (ClientSociete) this._listeClientSociete.get(i);
                        if (tampon.getNomSociete().contains(nomSociete))
                            return tampon;
                        }
                }
                return null;
        }

        public synchronized Vector<ClientSociete> rechercherSocieteAdresse( String rue, String numero, 
                                                                            String codePostal, String commune) 
                                                                            throws RemoteException, 
                                                                            AdresseMinimumMultiplicityException, 
                                                                            CodePostalMinimumMultiplicityException {
                Vector<ClientSociete> _listeTampon = new Vector<ClientSociete>();
                for(int i = 0; i < this._listeClientSociete.size(); i++){
                    ClientSociete tampon = (ClientSociete) this._listeClientSociete.get(i);
                    if ((tampon.getAdresse().getRue().contains(rue))&&(tampon.getAdresse().getNumero().contains(numero))&&
                        (tampon.getAdresse().getCodePostal().getCodePostal().contains(codePostal))&&
                        (tampon.getAdresse().getCodePostal().getVille().contains(commune)))
                        _listeTampon.add(tampon);
                }
                return _listeTampon;
        }

        public boolean modifierClientParticulier(ClientParticulier client) throws RemoteException{
                for(int i = 0 ; i < this._listeClientParticulier.size() ; i++){
                        ClientParticulier tampon = (ClientParticulier) this._listeClientParticulier.get(i);
                        if ((tampon.getIdClient())== client.getIdClient()){
                                this._listeClientParticulier.remove(i);
                                this._listeClientParticulier.add(client);
                        return true;                    
                        }
                }
                return false;
        }
        
        public synchronized Client rechercherIdClient(int idClient) throws RemoteException {
                if (idClient > 0){
                    for (int i = 0; i < this._listeClient.size(); i++){
                        Client tampon = (Client) this._listeClient.get(i);
                        if (tampon.getIdClient() == idClient)
                            return tampon;
                    }
                }
                return null;
        }
        
        public synchronized int nombreDeClientsParticulier() throws RemoteException {
                return this._listeClientParticulier.size();
        }
        
        public synchronized int nombreDeClientsSociete() throws RemoteException {
                return this._listeClientSociete.size();
        }

        public synchronized int nombreDeClients() throws RemoteException {
                return this._listeClient.size();
        }
        
        public synchronized void remplirListeClientsParticulier(Vector<ClientParticulier> clientP) throws RemoteException {
                this._listeClientParticulier = clientP;
                this.notifyAllSubscibers();
        }
        
        public synchronized void remplirListeClientsSociete(Vector<ClientSociete> clientS) throws RemoteException {
                this._listeClientSociete = clientS;
                this.notifyAllSubscibers();
        }
        
        public synchronized void remplirListeClients() throws RemoteException {
                if (this._listeClientParticulier.isEmpty() == false){
                    for (int i = 0 ; i < _listeClientParticulier.size(); i++){
                        Client tampon = (Client) _listeClientParticulier.get(i);
                        this._listeClient.add(tampon);
                    }
                    this.notifyAllSubscibers();
                }
                if (this._listeClientSociete.isEmpty() == false){
                    for (int i = 0 ; i < _listeClientSociete.size(); i++){
                        Client tampon = (Client) _listeClientSociete.get(i);
                        this._listeClient.add(tampon);                
                    }
                    this.notifyAllSubscibers();                    
                }
        }
        
        public synchronized Vector<ClientParticulier> getListeClientParticulier() throws RemoteException {
                return this._listeClientParticulier;
        }
        
        public synchronized Vector<ClientSociete> getLlisteClientSociete() throws RemoteException {
            return this._listeClientSociete;
        }
        
	//*******************************************************************
	//Associations -> ClientParticulier
	//*******************************************************************
        
        public synchronized boolean ajouterClientParticulier(ClientParticulier client) throws RemoteException{
                if (client ==  null) return false;
                if (instance.contientClientParticulier(client)) return false;
                if (!instance.peutAjouterClientParticulier()) return false;
                instance._listeClientParticulier.add(client);
                this.notifyAllSubscibers();
                return true;
        }
    
        public synchronized boolean supprimerClientParticulier(ClientParticulier client) throws RemoteException{
                if(client==null)return false;
                if(!instance.contientClientParticulier(client))return false;
                instance._listeClientParticulier.remove(client);
                this.notifyAllSubscibers();                
                return true;
        }
    
        public boolean peutAjouterClientParticulier(){
                return true;
        }
    
        public boolean contientClientParticulier(ClientParticulier client){
                return instance._listeClientParticulier.contains(client);
        }

	//*******************************************************************
	//Associations -> ClientSociete
	//*******************************************************************        
        
        public synchronized boolean ajouterClientSociete(ClientSociete client) throws RemoteException{
                if (client ==  null) return false;
                if (instance.contientClientSociete(client)) return false;
                if (!instance.peutAjouterClientSociete()) return false;
                instance._listeClientSociete.add(client);
                this.notifyAllSubscibers();
                return true;
        }
    
        public synchronized boolean supprimerClientSociete(ClientSociete client) throws RemoteException{
                if(client==null)return false;
                if(!instance.contientClientSociete(client))return false;
                instance._listeClientSociete.remove(client);
                this.notifyAllSubscibers();
                return true;
        }
    
        public boolean peutAjouterClientSociete(){
                return true;
        }
    
        public boolean contientClientSociete(ClientSociete client){
                return instance._listeClientSociete.contains(client);
        }
        
	//*******************************************************************
	//Associations -> Client
	//*******************************************************************
        
        public synchronized boolean ajouterClient(Client client) throws RemoteException{
                if (client ==  null) return false;
                if (instance.contientClient(client)) return false;
                if (!instance.peutAjouterClient()) return false;
                instance._listeClient.add(client);
                this.notifyAllSubscibers();
                return true;
        }        

        public boolean peutAjouterClient(){
                return true;
        }
    
        public boolean contientClient(Client client){
                return instance._listeClient.contains(client);
        }        
        
}